package com.theexceptions.todo.controller;

import com.theexceptions.todo.model.DatabaseHelper;
import com.theexceptions.todo.model.IPersister;

/**
 * A controller that handles all user registration operations.
 * 
 * @author Justian Meyer and JD Reddaway
 */
public abstract class RegisterController extends Controller {

	public static final int USERNAME_INVALID = 0;
	public static final int REGISTER_SUCCESS = 1;

	/**
	 * Attempts to register a user into the system with the provided information
	 * and returns an integer detailing the success of the action.
	 * 
	 * @param username Username provided by the user
	 * @param password Password provided by the user
	 * @param name Name provided by the user
	 * @param email Email provided by the user
	 * @return USERNAME_INVALID if username already exists in the database,
	 *         REGISTER_SUCCESS if the registration is successful
	 */
	public static int registerUser(String username, String password, String name, String email) {
		IPersister dh = new DatabaseHelper(context);
		long rowID = dh.insertUser(username, password, name, email);

		if (rowID < 0)
			return USERNAME_INVALID;
		else {
			dh.insertCategory(rowID, "Work");
			dh.insertCategory(rowID, "School");
			dh.insertCategory(rowID, "Personal");
			return REGISTER_SUCCESS;
		}
	}
}
